﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.Client.frms;

namespace ECare.Win.controls
{
    public partial class ucRoles : DevExpress.XtraEditors.XtraUserControl
    {
        public ucRoles()
        {
            InitializeComponent();

            this.Load += ucRoles_Load;

            this.windowsUIButtonPanel1.ButtonClick += windowsUIButtonPanel1_ButtonClick;

            this.eGridView1.CellDoubleClick += eGridView1_CellDoubleClick;
        }

        void eGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //throw new NotImplementedException();

            if (e.RowIndex == -1)
                return;

            var entitye = this.eGridView1.Rows[e.RowIndex].DataBoundItem as RoleInfo;
            var frm = new frmRoleInfo {entity = entitye};
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }

        void windowsUIButtonPanel1_ButtonClick(object sender, DevExpress.XtraBars.Docking2010.ButtonEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.Button.Properties.Caption == "Edit")
            {
                if (this.eGridView1.SelectedRows.Count == 0)
                    return;

                var entitye = this.eGridView1.SelectedRows[0].DataBoundItem as RoleInfo;
                var frm = new frmRoleInfo();
                frm.entity = entitye;
                frm.Saved += frm_Saved;
                frm.ShowDialog();
            }else if (e.Button.Properties.Caption == "New")
            {
                var frm = new frmRoleInfo();
                frm.entity = null;
                frm.Saved += frm_Saved;
                frm.ShowDialog();
            }
            else
            {
                

            }

        }



        public BindingList<RoleInfo> source { get; set; }
        void ucRoles_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            source = new BindingList<RoleInfo>(RoleInfoHelper.GetRoles());
            this.eGridView1.DataSource = source;
        }


        void frm_Saved(object entity)
        {
            if (source == null)
                source = new BindingList<RoleInfo>();
            var role = entity as RoleInfo;
            var findresult = source.FirstOrDefault(a => a.RoleId == role.RoleId);
            if (findresult == null)
                this.source.Add(role);
        }
    }
}
